<?php


namespace api\applets\controller;


use api\applets\model\SMSTool;
use cmf\controller\RestBaseController;
use think\Db;
use think\facade\Cache;

/**
 * 个人中心
 * Class PersonalController
 * @package api\applets\controller
 */
class PersonalController extends RestBaseController
{
    /**
     * 发送验证码
     */
    public function send()
    {
        $request = $this->request;
        $phone = $request->param("phone");
        if (empty($phone)) {
            $this->error("请填写手机号");
        }
        $alisms = new SMSTool();
        $flag = $alisms->sendSms($phone);
        if ($flag) {
            $this->success("发送成功！");
        } else {
            $this->error("发送失败！");
        }
    }

    /**
     * 验证验证码
     */
    public function VerificationCode($code)
    {

        $sessionCode = Cache::get("code");
        if (empty($sessionCode)) {
            $this->error("验证码已过期！");
        }
        if ($code != $sessionCode) {
            $this->error("验证码错误！");
        }
    }

    /**
     * 验证购墓人表里有没有数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function verificationCustomer($name, $cardID, $tomb_number, $phone)
    {

        // 根据条件查询购墓人表
        $customer = Db::name("customer_info")->alias("info")
            ->field("info.id ,info.buyer_phone,o.order_id ")
            ->leftJoin("order o", "o.customer_id = info.id")
            ->where(array("info.is_dy_mz" => "1", "info.buyer_name" => $name, "info.customer_account" => $cardID, "info.tomb_number" => $tomb_number))->find();
//        echo Db::name("customer_info")->getLastSql();die;
        if (!empty($customer)) {
            // 如果传过来的手机号和查出来的手机号不一样就更新手机号
            if ($customer['buyer_phone'] != $phone) {
                $customer['buyer_phone'] = $phone;
                $this->update($customer);
            }
        } else {
            $this->error("没有查到信息！");
        }
        return $customer['order_id'];


    }

    /**
     * 更新手机号
     * @param $customer
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update($customer)
    {
        if (empty($customer)) {
            Db::name("customer_info")->where("id", $customer['id'])->update($customer);
        }
    }

    /**
     * 提交绑定
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function commitBindingGrave()
    {
        $request = $this->request;
        // 接收墓穴编号
        $tomb_number = $request->param("tomb_number");
        // 接收购墓人姓名
        $name = $request->param("name");
        // 接收购墓人身份证号
        $cardID = $request->param("cardID");
        // 接收购墓人手机号
        $phone = $request->param("phone");
        // 接收验证码
        $code = $this->request->param("code");
        $openId = $request->param("openId");

        // 验证验证码是否正确
        $this->VerificationCode($code);
        // 验证购墓人表里有没有数据，更新手机号
        $order_id = $this->verificationCustomer($name, $cardID, $tomb_number, $phone);
        $flag = Db::name("order")->where("order_id", $order_id)->update(['openID' => $openId]);
        if ($flag == 1) {
            $this->success("绑定成功！");
        } else {
            $this->error("绑定失败！");
        }

    }

    /**
     * 查询已购墓穴
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getTombsPurchased(){
        $request = $this->request;
        $openId = $request->param("openID");
        $data = Db::name("order")->alias("o")->distinct(true)
            ->field("info.tomb_number,p.tomb_region,ad.deceased_name,info.buyer_name")
            ->join("customer_info info","o.customer_id = info.id","left")
            ->join("tombarea_row_col co","o.tomb_id = co.colID","left")
            ->join("archives_deceased ad","o.order_id = ad.order_id","left")
            ->join("tombarea_information_product p","co.colID = p.tomb_id","left")
            ->where(array("o.openID"=>$openId,"info.is_dy_mz"=>1))
            ->select();
//        $sql = Db::name("order")->getLastSql();;
////        $this->success("请求成功！",$sql);
//        $data['sql'] = $sql;
        $this->success("请求成功！",$data);

    }
}